package com.zingrow.web.report.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.idaycrm.idaybase.utils.JsonUtils;
import com.zingrow.common.ReturnStandardDataFormat;
import com.zingrow.common.exception.NumberErrorException;
import com.zingrow.common.utils.EncodingUtils;
import com.zingrow.common.utils.MongoHelper;
import com.zingrow.common.utils.OptClass;
import com.zingrow.common.utils.StringUtil;
import com.zingrow.web.base.CodeMsg;
import com.zingrow.web.base.controller.BaseController;
import com.zingrow.web.base.enums.Menu;
import com.zingrow.web.base.enums.Operation;
import com.zingrow.web.configure.distribute.model.InformationDistributeModel;
import com.zingrow.web.configure.distribute.service.IInformationDistributeService;
import com.zingrow.web.configure.iformation.service.IIformationService;
import com.zingrow.web.entity.InformationDistribute;
import com.zingrow.web.report.base.Cycle;
import com.zingrow.web.report.service.IRptStatisticsService;
import com.zingrow.web.support.operate.model.OptRecord;
import com.zingrow.web.support.operate.service.IOptRecordService;
import com.zingrow.web.user.model.ActiveUser;

@Controller
@RequestMapping(value = "/RptStatistics/", produces = "application/json;charset=UTF-8")
public class RptStatisticsController extends BaseController {

	@Autowired
	private IRptStatisticsService rptStatisticsService;
	@Autowired
	private IInformationDistributeService iInformationDistributeService;
	@Autowired
	private IOptRecordService optRecordService;
	@Autowired
	private IIformationService iformationService;
	@Autowired
	private IOptRecordService ioptRecordService;

	MongoHelper mongo = MongoHelper.getInstance();

	/**
	 * 新增到站点维度统计的数据表
	 */

	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 站点
	 * 
	 * @param httpRequest
	 * @param response
	 * @param ifmIds
	 * @param StatisticsTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("insterDimensionDomain")
	public String insterDimensionDomain(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String ifmIds, @RequestParam(required = false) String StartTime,
			@RequestParam(required = false) String endTime, @RequestParam(required = false) Integer month) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {

			// for (int i = 15; i < 16; i++) {
			// for (int i = 1; i < 4; i++) {
			StartTime = "2017-" + 7 + "-" + month + " 15:15:15";
			endTime = "2017-" + 8 + "-" + 1 + " 15:15:15";
			// StartTime = "2017-" + 8 + "-" + num + " 15:15:15";
			// endTime = "2017-" + 7 + "-" + (num+7) + " 15:15:15";
			// endTime = "2017-" + 8 + "-" + 13 + " 15:15:15";
			if (StringUtils.isNotBlank(ifmIds)) {
				ifmIds = EncodingUtils.fixUrlParamEncoding(httpRequest, ifmIds);
				rptStatisticsService.insterDimensionDomain(StringUtil.getStringToList(ifmIds),
						StringUtils.isNotBlank(StartTime) ? format.parse(StartTime) : null,
						StringUtils.isNotBlank(endTime) ? format.parse(endTime) : null, Cycle.MONTH.getCode());
			} else {
				List<Integer> nullifnIds = new ArrayList<>();
				rptStatisticsService.insterDimensionDomain(nullifnIds,
						StringUtils.isNotBlank(StartTime) ? format.parse(StartTime) : null,
						StringUtils.isNotBlank(endTime) ? format.parse(endTime) : null, Cycle.MONTH.getCode());
			}
			// }
		} catch (Exception e) {
			log.error("统计站点发生异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 渠道
	 * 
	 * @param httpRequest
	 * @param response
	 * @param ifmIds
	 * @param StatisticsTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("insterChannel")
	public String insterChannel(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String ifmIds, @RequestParam(required = false) String StatisticsTime,
			int month) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);

		try {

			for (int i = 15; i < 16; i++) {
				StatisticsTime = "2017-" + month + "-" + i + " 15:15:15";
				if (StringUtils.isNotBlank(ifmIds)) {
					ifmIds = EncodingUtils.fixUrlParamEncoding(httpRequest, ifmIds);
					rptStatisticsService.insterChannel(StringUtil.getStringToList(ifmIds),
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				} else {
					List<Integer> nullifnIds = new ArrayList<>();
					rptStatisticsService.insterChannel(nullifnIds,
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				}
			}

		} catch (Exception e) {
			log.error("统计维度发生异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 方案统计维度
	 * 
	 * @param httpRequest
	 * @param response
	 * @param ifmIds
	 * @param StatisticsTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("insterDimensionPlan")
	public String insterDimensionPlan(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String ifmIds, @RequestParam(required = false) String StatisticsTime,
			int month) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {
			for (int i = 15; i < 16; i++) {
				StatisticsTime = "2017-" + month + "-" + i + " 15:15:15";
				if (StringUtils.isNotBlank(ifmIds)) {
					ifmIds = EncodingUtils.fixUrlParamEncoding(httpRequest, ifmIds);
					rptStatisticsService.insterPlan(StringUtil.getStringToList(ifmIds),
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				} else {
					List<Integer> nullifnIds = new ArrayList<>();
					rptStatisticsService.insterPlan(nullifnIds,
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				}
			}
		} catch (Exception e) {
			log.error("统计方案维度发生异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 方案排行
	 * 
	 * @param httpRequest
	 * @param response
	 * @param ifmIds
	 * @param StatisticsTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("insterPlanRanking")
	public String insterPlanRanking(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String ifmIds, @RequestParam(required = false) String StatisticsTime,
			int month) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {
			for (int i = 1; i < 24; i++) {
				StatisticsTime = "2017-" + month + "-" + i + " 15:15:15";
				if (StringUtils.isNotBlank(ifmIds)) {
					ifmIds = EncodingUtils.fixUrlParamEncoding(httpRequest, ifmIds);
					rptStatisticsService.insterPlanRanking(StringUtil.getStringToList(ifmIds),
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				} else {
					List<Integer> nullifnIds = new ArrayList<>();
					rptStatisticsService.insterPlanRanking(nullifnIds,
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				}
			}
		} catch (Exception e) {
			log.error("统计方案排行发生异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 方案新闻排行
	 * 
	 * @param httpRequest
	 * @param response
	 * @param ifmIds
	 * @param StatisticsTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("insterNewsRanking")
	public String insterNewsRanking(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String ifmIds, @RequestParam(required = false) String StatisticsTime,
			int month) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {
			for (int i = 26; i < 30; i++) {
				StatisticsTime = "2017-" + month + "-" + i + " 15:15:15";
				if (StringUtils.isNotBlank(ifmIds)) {
					ifmIds = EncodingUtils.fixUrlParamEncoding(httpRequest, ifmIds);
					rptStatisticsService.insterNewsRanking(StringUtil.getStringToList(ifmIds),
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				} else {
					List<Integer> nullifnIds = new ArrayList<>();
					rptStatisticsService.insterNewsRanking(nullifnIds,
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				}
			}
		} catch (Exception e) {
			log.error("统计方案新闻排行发生异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 方案微信排行
	 * 
	 * @param httpRequest
	 * @param response
	 * @param ifmIds
	 * @param StatisticsTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("insterWeixinRanking")
	public String insterWeixinRanking(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String ifmIds, @RequestParam(required = false) String StatisticsTime,
			int month) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {
			for (int i = 15; i < 16; i++) {
				StatisticsTime = "2017-" + month + "-" + i + " 15:15:15";
				if (StringUtils.isNotBlank(ifmIds)) {
					ifmIds = EncodingUtils.fixUrlParamEncoding(httpRequest, ifmIds);
					rptStatisticsService.insterWeixinRanking(StringUtil.getStringToList(ifmIds),
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				} else {
					List<Integer> nullifnIds = new ArrayList<>();
					rptStatisticsService.insterWeixinRanking(nullifnIds,
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				}
			}
		} catch (Exception e) {
			log.error("统计方案微信排行发生异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 方案微博排行
	 * 
	 * @param httpRequest
	 * @param response
	 * @param ifmIds
	 * @param StatisticsTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("insterWeiboRanking")
	public String insterWeiboRanking(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String ifmIds, @RequestParam(required = false) String StatisticsTime,
			int month) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {
			for (int i = 1; i < 24; i++) {
				StatisticsTime = "2017-" + month + "-" + i + " 15:15:15";
				if (StringUtils.isNotBlank(ifmIds)) {
					ifmIds = EncodingUtils.fixUrlParamEncoding(httpRequest, ifmIds);
					rptStatisticsService.insterWeiboRanking(StringUtil.getStringToList(ifmIds),
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				} else {
					List<Integer> nullifnIds = new ArrayList<>();
					rptStatisticsService.insterWeiboRanking(nullifnIds,
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				}
			}
		} catch (Exception e) {
			log.error("统计方案微博排行发生异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 方案论坛排行
	 * 
	 * @param httpRequest
	 * @param response
	 * @param ifmIds
	 * @param StatisticsTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("insterBbsRanking")
	public String insterBbsRanking(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String ifmIds, @RequestParam(required = false) String StatisticsTime,
			int month) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		try {
			for (int i = 15; i < 16; i++) {
				StatisticsTime = "2017-" + month + "-" + i + " 15:15:15";
				if (StringUtils.isNotBlank(ifmIds)) {
					ifmIds = EncodingUtils.fixUrlParamEncoding(httpRequest, ifmIds);
					rptStatisticsService.insterWeiboRanking(StringUtil.getStringToList(ifmIds),
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				} else {
					List<Integer> nullifnIds = new ArrayList<>();
					rptStatisticsService.insterBbsRanking(nullifnIds,
							StringUtils.isNotBlank(StatisticsTime) ? format.parse(StatisticsTime) : null);
				}
			}
		} catch (Exception e) {
			log.error("统计方案论坛排行发生异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 重新统计
	 * 
	 * @param httpRequest
	 * @param response
	 * @param map
	 * @return
	 * @throws NumberErrorException
	 */
	@ResponseBody
	@RequestMapping("deleteAndInsterStatistics")
	public String deleteAndInsterStatistics(final HttpServletRequest httpRequest, HttpServletResponse response,
			final @RequestParam(required = false) Integer ifmId, @RequestParam(required = false) String date,
			final @RequestParam(required = false) String groupId, @RequestParam(required = false) String releaseTime1,
			@RequestParam(required = false) String releaseTime2,@RequestParam(required = false) String ifmids,
			@RequestParam(required = false) String title,
			@RequestParam(required = false) String ifmIda,
			@RequestParam(required = false) String articleId,
			@RequestParam(required = false) String ea) throws NumberErrorException {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		Map<Integer, Date> map = new HashMap<Integer, Date>();
		List<String> groupIds=new ArrayList<String>();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date startTime = null;
		Date endTime = null;
		String[] ifmidsa = null;
		String grid = "";
		try {
			if (title != "" || "".equals(title)) {
				title = EncodingUtils.fixUrlParamEncoding(httpRequest, title);
			}
			InformationDistributeModel buteModel = new InformationDistributeModel();
			InformationDistributeModel buteModel2 = new InformationDistributeModel();		
			buteModel = iInformationDistributeService.selectByid(
						InformationDistributeModel.class,articleId,ifmIda.toString());			
			if(!ea.equals("dd"))
			{buteModel2 = iInformationDistributeService.selectByid(
						InformationDistributeModel.class,groupId,ifmids.toString());}
			    //将groupid分到list中
			if(StringUtils.isNotBlank(groupId)){
			    String[] groupids=groupId.split(",");
			   
			    for(int i=0;i<groupids.length;i++) 
			    	groupIds.add(groupids[i]);
					grid=groupIds.get(0);
			}
			if(StringUtils.isNotBlank(ifmids)){
				ifmidsa=ifmids.split(",");
			}
				List<Integer> ifmidsas=new ArrayList<Integer>();
			if (StringUtils.isNotBlank(grid)) {
				if (StringUtils.isNotBlank(releaseTime1) && StringUtils.isNotBlank(releaseTime2)) {
					startTime = dateFormat.parse(releaseTime1);
					endTime = dateFormat.parse(releaseTime2);
				}
				Set<InformationDistribute> set = null;
				//判断是否为批量删除
				if(groupIds.size()>1)
				{   for(int i=0;i<ifmidsa.length;i++) ifmidsas.add(Integer.valueOf(ifmidsa[i]));
					set = mongo.queryByGroupId(InformationDistribute.class, groupIds, startTime,
						endTime, ifmidsas);
			    }
				else
				{set = mongo.queryByGroupId(InformationDistribute.class, grid, startTime,
						endTime, ifmId);}
				final Date startTime1 = startTime;
				final Date endTime1 = endTime;
				if (iInformationDistributeService.deleteByField(InformationDistributeModel.class, groupIds, startTime1,
						endTime1, ifmId != null ? ifmId.toString() : null)) {
				}
				if (set != null && set.size() > 0) {
					for (InformationDistribute informationDistribute : set) {
						map.put(Integer.valueOf(informationDistribute.getIfmId()),
								informationDistribute.getReleaseTime());
					}
				}
			if (rptStatisticsService.deleteAndInsterStatistics(map)) {
					//新增一条操作记录
					//操作日志
					ActiveUser opuser = getUser(httpRequest);//获取操作用户
					String ifms=null,groups=null;
					List<String> lists=new ArrayList<String>();
		
					if(groupIds.size()>1)
					{
						OptRecord s = new OptClass().goiss(opuser,Menu.informationmanagement.toString(),
								Menu.distribute.toString(),Operation.deldetes.toString(),groupIds.size());
						if(s.getOptName()!="null")
						ioptRecordService.insert(s);
					}
					else
					{
						if(ea.equals("delAll"))
						{
							if(buteModel2.getTitle()!=null)
							{
							OptRecord s = new OptClass().dist_delete(opuser,Menu.informationmanagement.toString(),
									Menu.distribute.toString(),Operation.delete.toString(),buteModel2.getTitle(),"1");
							if(s.getOptName()!="null")
							ioptRecordService.insert(s);
							}
							else
							{
								OptRecord s = new OptClass().dist_delete(opuser,Menu.informationmanagement.toString(),
										Menu.distribute.toString(),Operation.delete.toString(),buteModel2.getUrl(),"2");
								if(s.getOptName()!="null")
								ioptRecordService.insert(s);	
							}
						}else
						{
						if(title!=null&&!"".equals(title)&&!"null".equals(title))
						{
						OptRecord s = new OptClass().dist_delete(opuser,Menu.informationmanagement.toString(),
								Menu.distribute.toString(),Operation.delete.toString(),title,"1");
						if(s.getOptName()!="null")
						ioptRecordService.insert(s);
						}
						else 
						{
						OptRecord s = new OptClass().dist_delete(opuser,Menu.informationmanagement.toString(),
								Menu.distribute.toString(),Operation.delete.toString(),buteModel.getUrl(),"2");
						if(s.getOptName()!="null")
						ioptRecordService.insert(s);
						}
						}
					
				}
					standardData.setReturnCode(CodeMsg.CODE_OK);
					standardData.setReturnMessage("重新统计成功");
					return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			} else {
				}
			} else if(!ea.equals("dd")){
				if (StringUtils.isNotBlank(date)) {
					try {
						Date dateTime = dateFormat.parse(date);
						map.put(ifmId, dateTime);
						rptStatisticsService.deleteAndInsterStatistics(map);
						if(ea.equals("delAll"))
						{
							if(buteModel2.getTitle()!=null)
							{
								ActiveUser opuser = getUser(httpRequest);//获取操作用户
							OptRecord s = new OptClass().dist_delete(opuser,Menu.informationmanagement.toString(),
									Menu.distribute.toString(),Operation.delete.toString(),buteModel2.getTitle(),"1");
							if(s.getOptName()!="null")
							ioptRecordService.insert(s);
							}
							else
							{ActiveUser opuser = getUser(httpRequest);//获取操作用户
								OptRecord s = new OptClass().dist_delete(opuser,Menu.informationmanagement.toString(),
										Menu.distribute.toString(),Operation.delete.toString(),buteModel2.getUrl(),"2");
								if(s.getOptName()!="null")
								ioptRecordService.insert(s);	
							}
						}else
						{
						if(title!=null&&!"".equals(title)&&!"null".equals(title))
						{
						//操作日志
						ActiveUser opuser = getUser(httpRequest);//获取操作用户
						OptRecord s = new OptClass().dist_delete(opuser,Menu.informationmanagement.toString(),
									Menu.distribute.toString(),Operation.delete.toString(),title,"1");
						if(s.getOptName()!="null")
						ioptRecordService.insert(s);
						}
						else
						{
						ActiveUser opuser = getUser(httpRequest);//获取操作用户
						OptRecord s = new OptClass().dist_delete(opuser,Menu.informationmanagement.toString(),
								Menu.distribute.toString(),Operation.delete.toString(),buteModel.getUrl(),"2");
						if(s.getOptName()!="null")
						ioptRecordService.insert(s);
						}}
					} catch (ParseException e) {
						e.printStackTrace();
					}
				} else {
					standardData.setReturnCode(CodeMsg.CODE_ERROR);
					standardData.setReturnMessage(CodeMsg.MSG_ERROR);
					return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
				}

			}
		} catch (Exception e) {
			log.error("重新统计发生异常");
			ActiveUser opuser = getUser(httpRequest);//获取操作用户
			OptRecord s = new OptClass().recordabnormality(opuser,"已发布信息重新统计发生异常");
			ioptRecordService.insert(s);
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage("重新统计成功");
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 删除站点统计表信息
	 * 
	 * @param httpRequest
	 * @param response
	 * @param ifmIds
	 * @param StatisticsTime
	 * @return
	 */
	@ResponseBody
	@RequestMapping("deleyeDimensionDomain")
	public String deleyeDimensionDomain(HttpServletRequest httpRequest, HttpServletResponse response,
			@RequestParam(required = false) String ifmIds, @RequestParam(required = false) String StatisticsTime) {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		Map<Integer, Date> map = new HashMap<Integer, Date>();
		List<String> datelist = new ArrayList<>();
		try {

			if (StringUtils.isNotBlank(ifmIds)) {
				ifmIds = EncodingUtils.fixUrlParamEncoding(httpRequest, ifmIds);
				List<Integer> ifmidlist = StringUtil.getStringToList(ifmIds);
				if (StringUtils.isNotBlank(StatisticsTime)) {
					datelist = StringUtil.toStringTokenizer(StatisticsTime, ",");
					for (int i = 0; i < ifmidlist.size(); i++) {
						map.put(ifmidlist.get(i),
								StringUtils.isNotBlank(datelist.get(i)) ? format.parse(datelist.get(i)) : null);
					}
				} else {
					for (int i = 0; i < ifmidlist.size(); i++) {
						map.put(ifmidlist.get(i), null);
					}
				}
				rptStatisticsService.deleteStatistics(map);
			} else {
				log.error("必须传入方案ID");
				standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
				standardData.setReturnMessage("必须传入方案ID");
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
		} catch (Exception e) {
			log.error("删除站点统计表发生异常");
			log.error(e.getMessage());
			standardData.setReturnCode(CodeMsg.CODE_EXCEPTION);
			standardData.setReturnMessage(CodeMsg.MSG_EXCEPTION);
			return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
		}
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage(CodeMsg.MSG_OK);
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	/**
	 * 重新八月统计
	 * 
	 * @param httpRequest
	 * @param response
	 * @param map
	 * @return
	 * @throws NumberErrorException
	 * @throws ParseException
	 */
	@ResponseBody
	@RequestMapping("abc")
	public String abc(final HttpServletRequest httpRequest, HttpServletResponse response)
			throws NumberErrorException, ParseException {
		ReturnStandardDataFormat standardData = new ReturnStandardDataFormat(CodeMsg.CODE_ERROR, CodeMsg.MSG_ERROR,
				null);
		List<Integer> ifmIds = iformationService.getAllId();
		// Calendar cal = Calendar.getInstance();
		String start = "2017-08-01 00:00:00";
		String end = "2017-08-23 00:00:00";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dBegin = sdf.parse(start);
		Date dEnd = sdf.parse(end);
		List<Date> lDate = findDates(dBegin, dEnd);
		for (Date date : lDate) {
			Map<Integer, Date> map = new HashMap<Integer, Date>();
			for (Integer ifmId : ifmIds) {
				map.put(ifmId, date);
			}
			if (rptStatisticsService.deleteAndInsterStatistics(map)) {
			} else {
				standardData.setReturnCode(CodeMsg.CODE_ERROR);
				standardData.setReturnMessage(CodeMsg.MSG_ERROR);
				return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
			}
		}
		standardData.setReturnCode(CodeMsg.CODE_OK);
		standardData.setReturnMessage("重新统计成功");
		return JsonUtils.toString(standardData, "yyyy-MM-dd HH:mm:ss");
	}

	public static List<Date> findDates(Date dBegin, Date dEnd) {
		List lDate = new ArrayList();
		lDate.add(dBegin);
		Calendar calBegin = Calendar.getInstance();
		// 使用给定的 Date 设置此 Calendar 的时间
		calBegin.setTime(dBegin);
		Calendar calEnd = Calendar.getInstance();
		// 使用给定的 Date 设置此 Calendar 的时间
		calEnd.setTime(dEnd);
		// 测试此日期是否在指定日期之后
		while (dEnd.after(calBegin.getTime())) {
			// 根据日历的规则，为给定的日历字段添加或减去指定的时间量
			calBegin.add(Calendar.DAY_OF_MONTH, 1);
			lDate.add(calBegin.getTime());
		}
		return lDate;
	}

	public static void main(String[] args) throws ParseException {
		Calendar cal = Calendar.getInstance();
		String start = "2017-08-01 00:00:00";
		String end = "2017-08-22 00:00:00";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date dBegin = sdf.parse(start);
		Date dEnd = sdf.parse(end);
		List<Date> lDate = findDates(dBegin, dEnd);
		for (Date date : lDate) {
			System.out.println(sdf.format(date));
		}
	}

}
